package com.neo.service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.neo.util.DjsStringUtil;
import com.neo.util.RandomUserNameUtil;
import com.neo.util.ReadFromFile;


@Service
public class TransferLoanProductInfoService {

    protected static Logger logger= LoggerFactory.getLogger(TransferLoanProductInfoService.class); 
    
	@Autowired
    private JdbcTemplate jdbcTemplate;
	@Autowired
	private RandomUserNameUtil randomUserNameUtil;
	
	//CVS文件插入jx_inv_borrower_message表
	public void insertInvUser() throws Exception {
		//List listCsv = ReadFromFile.readFileByLinesGbk(DjsStringUtil.IMPORT_FILE_PATH);
		List<String> listCsv = ReadFromFile.readFileByScanner(DjsStringUtil.IMPORT_FILE_PATH);
		
		int res=0;
		for (int i = 1; i < listCsv.size(); i++) {
		Map<String,Object> map = new HashMap();
			String str = listCsv.get(i);
			String[] s = str.split(",");

			map.put("CREDITOR_CODE", s[0]);//债权编号
			map.put("BORROWER_NAME", s[12]);//借款人姓名
			map.put("BORROWER_IDCARDNUM", s[13]);//借款人身份证号
			map.put("BORROWER_SEX", s[1]);//
			map.put("BORROWER_AGE", s[2]);//
			map.put("BORROWER_CITY", s[3]);//
			map.put("BORROWER_OCCUPATION", s[7]);//职业
			map.put("BORROWER_INDUSTRY", s[7]);//行业
			map.put("BORROWER_COMPANY", s[15]);//公司
			map.put("LOAN_AMT", s[17]);
			//map.put("REPAY_TYPE", s[0]);
			map.put("SIGNING_DATE", s[4]);
			
			updateByMap(map);
			logger.info("结果:"+res+"=="+i);
				
		}
		logger.info("结果文件数:"+res);
	}
	
	
	//智能拼接sql 
	private int updateByMap(Map<String,Object> map) {
		Object[] arg = new Object[map.keySet().size()];
		String sql = DjsStringUtil.generateInsertSql(map,arg,"jx_inv_borrower_message"); 
		int j = jdbcTemplate.update(sql,arg);
		return j;
	}
	
}
